{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dedf6b39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thank you for your interest in this exam.\n",
      "hint: press QUIT or quit to quit (ABCD or abcd for your choice).\n",
      "Press Enter to continue:\n",
      ">>a\n",
      "You have one chance per question\n",
      "You cannot go back to answer a previous question\n",
      "The total number of questions is 10\n",
      "Press any key to start\n",
      "QUIZ STARTS NOW\n",
      "........................\n",
      "\n",
      "Which is not included in the Four Great Inventions of China?\n",
      "A.The printing press\n",
      "B.Gunpowder\n",
      "C.The compass\n",
      "D.Mathematic\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is D. Mathematic\n",
      "\n",
      "Where is the capital of China?\n",
      "A.Hongkong\n",
      "B.Shanghai\n",
      "C.Shenzhen\n",
      "D.Beijing\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is D. Beijing\n",
      "\n",
      "Which of the following is not China is obvious?\n",
      "A.Jackie chan\n",
      "B.Jay Chou\n",
      "C.Yi-bo wang\n",
      "D.Lee MinHo\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is D. Lee MinHo\n",
      "\n",
      "What is the color of the flag of China?\n",
      "A.Red and Black\n",
      "B.Yellow and Gold\n",
      "C.Red and Yellow\n",
      "D.Red and White\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is D. Red and White\n",
      "\n",
      "Which city in China will the Olympic Games be held?\n",
      "A.ChongQing\n",
      "B.Beijing\n",
      "C.ShangHai\n",
      "D.Shenzhen\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is B. Beijing\n",
      "\n",
      "Which of the following is not made in China?\n",
      "A.High-speed railway\n",
      "B.Shared electric vehicle\n",
      "C.Mobile payment\n",
      "D.Bitcoin\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is D. Bitcoin\n",
      "\n",
      "Which of the following schools is Chinese?\n",
      "A.Seoul national university\n",
      "B.The university of Sydney\n",
      "C.The university of Tokyo\n",
      "D.Peking University\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is D. Peking University\n",
      "\n",
      "What is China's area ranking in the world?\n",
      "A.1\n",
      "B.2\n",
      "C.3\n",
      "D.4\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is C. 3\n",
      "\n",
      "Which language is spoken by the largest number of Chinese?\n",
      "A.Mandarin\n",
      "B.French\n",
      "C.English\n",
      "D.Korean\n",
      "\n",
      ">>a\n",
      "Congratulations! your answer is correct!\n",
      "\n",
      "How many nationalities are there in China?\n",
      "A.55\n",
      "B.56\n",
      "C.57\n",
      "D.58\n",
      "\n",
      ">>a\n",
      "Wrong! The correct answer is B. 56\n",
      "\n",
      "Your score is:  1 . Your correct rate is:  10.0%\n",
      "You are welcome to take the test anytime. Bye!\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import csv\n",
    "\n",
    "def all_keys(my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: Collects all keys from the dictionary and stores them in a list\n",
    "    my_dict: The question dictionary\n",
    "    Return: returns the list created\n",
    "    \"\"\"\n",
    "    question_list = []\n",
    "    for key in my_dict.keys():\n",
    "        question_list.append(key)\n",
    "    return question_list\n",
    "\n",
    "def question_details(key, my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: Compacts the question details from the dictionary\n",
    "    kes: the key \n",
    "    my_dict: the dictionary\n",
    "    Return: returns question details in a list\n",
    "    \"\"\"\n",
    "    question = my_dict[key][\"questionText\"]\n",
    "    opt1 = my_dict[key][\"option1\"]\n",
    "    opt2 = my_dict[key][\"option2\"]\n",
    "    opt3 = my_dict[key][\"option3\"]\n",
    "    opt4 = my_dict[key][\"option4\"]\n",
    "    answer = my_dict[key][\"answer\"]\n",
    "    attempted = my_dict[key][\"attempted\"]\n",
    "    return [question, opt1, opt2, opt3, opt4, answer, attempted]\n",
    "\n",
    "def check_if_answered(key, my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: The function checks if a user has answered a specific question. \n",
    "    Helps to make sure that a question is not asked twice to the user in the same session\n",
    "    key: the key\n",
    "    my_dict: the dictionary\n",
    "    Return: returns True or False. True means the question is already answered,\n",
    "    \"\"\"\n",
    "    if int(my_dict[key][\"attempted\"]):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "def mark_attempted(key, my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: The function changes the value of the attempted property in the dictionary.\n",
    "    A question marked as attempted cannot be asked again\n",
    "    key: the key \n",
    "    my_dict: the dictionary\n",
    "    Return: returns None\n",
    "    \"\"\"\n",
    "    my_dict[key][\"attempted\"] = 1\n",
    "\n",
    "def shuffle_quiz(dictionary):\n",
    "    \"\"\"\n",
    "    Purpose: Shuffles the keys list and selects one randomly before fetching key details from the dictionary\n",
    "    dictionary: the dictionary (default=question_bank)\n",
    "    Return: returns a question from the dict question_array\n",
    "    \"\"\"\n",
    "    all_keys = list(question_bank.keys())\n",
    "    random.shuffle(all_keys)\n",
    "    shuffled_bank = {}\n",
    "    for key in all_keys:\n",
    "        shuffled_bank[key] = question_bank[key]\n",
    "    return shuffled_bank\n",
    "\n",
    "def read_data(filename):\n",
    "    question_bank = {}\n",
    "    f = open(filename,\"r\",encoding='utf-8')\n",
    "    questions = list(csv.reader(f))\n",
    "    for question in questions[1:]:\n",
    "        question_detail = {}\n",
    "        for i in range(1,len(question)):\n",
    "            question_detail[questions[0][i]] = question[i]\n",
    "        question_bank[question[0]] = question_detail\n",
    "    return question_bank\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    flag = True\n",
    "    scores = 0\n",
    "    #read data from csv format file\n",
    "    question_bank = read_data(\"about_china.csv\")\n",
    "    while flag:\n",
    "        choice = input(\"Thank you for your interest in this exam.\\nhint: press QUIT or quit to quit (ABCD or abcd for your choice).\\nPress Enter to continue:\\n>>\")\n",
    "        if choice == 'QUIT' or choice == 'quit':\n",
    "            flag = False\n",
    "        else:\n",
    "            print(\"You have one chance per question\\n\\\n",
    "You cannot go back to answer a previous question\\n\\\n",
    "The total number of questions is 10\\n\\\n",
    "Press any key to start\\n\\\n",
    "QUIZ STARTS NOW\\n\\\n",
    "........................\\n\")\n",
    "            answer_map = {\"a\": 0,\"A\": 0,\"b\": 1,\"B\": 1,\"c\": 2,\"C\": 2,\"d\": 3,\"D\": 3}\n",
    "            correct_list = [\"A\",\"B\",\"C\",\"D\"]\n",
    "            answered_number = 0\n",
    "            #one session have 10 questions\n",
    "            while answered_number < 10:\n",
    "                #shuffle the question\n",
    "                shuffled_bank = shuffle_quiz(question_bank)\n",
    "                #get the key of question bank\n",
    "                question_list=all_keys(shuffled_bank)\n",
    "                #check the question answered or not\n",
    "                if not check_if_answered(question_list[0], question_bank):\n",
    "                    details = question_details(question_list[0],shuffled_bank)\n",
    "                    #print out the question and items\n",
    "                    print(details[0] + \"\\n\" + \"A.\" + details[1] + \"\\n\" + \"B.\" + details[2] + \"\\n\"+ \"C.\" + details[3] + \"\\n\"+ \"D.\" + details[4] + \"\\n\")\n",
    "                    item = input(\">>\")\n",
    "                    if item == \"QUIT\" or item == \"quit\":\n",
    "                        flag = False\n",
    "                        break\n",
    "                    #require the user enter correctly\n",
    "                    while True:\n",
    "                        if item in \"ABCDabcd\":\n",
    "                            break\n",
    "                        else:\n",
    "                            print(\"please enter correctly!\\n\")\n",
    "                            item = input(\">>\")\n",
    "                \n",
    "                    #check the answer\n",
    "                    if details[1:5].index(details[5]) == answer_map[item]:\n",
    "                        scores += 1\n",
    "                        # mark the answered question\n",
    "                        mark_attempted(question_list[0],question_bank)\n",
    "                        answered_number += 1\n",
    "                        print(\"Congratulations! your answer is correct!\\n\")\n",
    "                    else:\n",
    "                        # mark the answered question\n",
    "                        mark_attempted(question_list[0],question_bank)\n",
    "                        answered_number += 1\n",
    "                        print(\"Wrong! The correct answer is \"+correct_list[details[1:5].index(details[5])]+\". \"+details[5]+\"\\n\")\n",
    "                    \n",
    "                    \n",
    "            flag = False\n",
    "    print(\"Your score is: \",scores,\".\",\"Your correct rate is: \",str((scores/10)*100)+\"%\")\n",
    "    print(\"You are welcome to take the test anytime. Bye!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfddbbd6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a306897",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
